//
//  CityListViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2016/11/10.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "CityListViewController.h"
#import "UserInfoViewController.h"
#import "ModifyUserInfoViewController.h"

@interface CityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation CityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"服务地区";
    self.tableArray = @[].mutableCopy;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CityCellID"];
    
    [HUDManager showLoadingHUDViewWithText:kMessage_NetLoading];
    [NetWork postNetWorkWithUrl:kURLCityList parame:@{@"provinceId":self.provinceId} successBlock:^(NSDictionary *dic) {
        NSLog(@"dic = %@", dic);
        [HUDManager hideHUDView];
        NSArray *cityList = dic[@"data"][@"cityList"];
        if (cityList.count) {
            self.tableArray = [CityModel arrayOfModelsFromDictionaries:cityList error:nil];
            [self.tableView reloadData];
        } else {
            [HUDManager showWarningWithText:@"暂无数据"];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:kMessage_NetError];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityModel *city = self.tableArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = city.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.type isEqualToString:@"1"]) {
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[UserInfoViewController class]]) {
                CityModel *city = self.tableArray[indexPath.row];
                NSDictionary *dic = @{@"cityId":city.cityId, @"name":[NSString stringWithFormat:@"%@ %@", self.provinceName, city.name]};
                //创建通知
                NSNotification *notification = [NSNotification notificationWithName:@"sendCity" object:nil userInfo:dic];
                //通过通知中心发送通知
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    } else {
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[ModifyUserInfoViewController class]]) {
                CityModel *city = self.tableArray[indexPath.row];
                NSDictionary *dic = @{@"cityId":city.cityId, @"name":[NSString stringWithFormat:@"%@ %@", self.provinceName, city.name]};
                //创建通知
                NSNotification *notification = [NSNotification notificationWithName:@"sendCityTwo" object:nil userInfo:dic];
                //通过通知中心发送通知
                [[NSNotificationCenter defaultCenter] postNotification:notification];
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    }
}


@end
